package com.qf.dao.impl;

import com.qf.dao.DeveloperDao;
import com.qf.pojo.Developer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName DeveloperDaoImpl
 * @Description TODO
 * @Author 86181
 * @Date 2022/9/2 14:13
 * @Version 1.0
 **/
@Repository("developerDao")
public class DeveloperDaoImpl implements DeveloperDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    public List<Developer> findAll() {
        List<Developer> query =
                jdbcTemplate.query("select * from developer", new BeanPropertyRowMapper<Developer>(Developer.class));
        for (Developer developer : query) {
            System.out.println(developer);
        }
        return query;
    }

    public Developer findDeveloperByDid(String did) {
        List<Developer> query
                = jdbcTemplate.query("select * from developer where did=?", new BeanPropertyRowMapper<Developer>(Developer.class), did);
        return query.get(0);
    }

    public int addDeveloper(Developer developer) {
        int update = jdbcTemplate.update("insert into developer(did,dname,updatetime) values (?,?,?)",developer.getDid(),developer.getUpdatetime(),developer.getDname());
        return update;
    }

    public int deleteDeveloper(int did) {
        int update = jdbcTemplate.update("delete* from developer where did=?", did);
        int i=0;
        if (update>0){
            i=jdbcTemplate.update("update project set did=null where did=?",did);
        }
        return i;
    }
}
